package org.arnetminer.personalized.service;

import java.util.ArrayList;
import java.util.List;

import org.arnetminer.personalized.dao.UserDAO;
import org.arnetminer.personalized.util.Global;
import org.arnetminer.personalized.util.Page;
import org.arnetminer.personalized.vo.Event;
import org.arnetminer.personalized.vo.EventType;
import org.arnetminer.personalized.vo.Organization;
import org.arnetminer.personalized.vo.Source;
import org.arnetminer.personalized.vo.User;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO;

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	private EventService eventService;

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public boolean isExist(User user) {
		
		List<User> users = userDAO.findByEmail(user.getEmail());
		if (users == null || users.size() == 0)
			return false;
		return true;
	}

	public boolean save(User user) {
		
		try {
			if (user.getId() == 0) {
				userDAO.save(user);
			} else {
				userDAO.save(user);

				Event event = new Event();
				event.setSubId(user.getId());
				event.setEventId(user.getId());
				EventType type = eventService.getEventType(Global.PROFILEEVENT);
				event.setType(type);
				eventService.save(event);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean validate(User user) {
		List properties = new ArrayList();
		List values = new ArrayList();

		List<User> users = userDAO.validate(user.getEmail(), user.getSecurity()
				.getPassword());
		if (users == null || users.size() == 0) {
			return false;
		}
		return true;
	}

	public User findUserById(long id) {
		
		User user = userDAO.findById(id);
		return user;
	}

	public User findUserByEmail(String email) {
		List users = userDAO.findByEmail(email);
		if (users != null && users.size() > 0) {
			return (User) users.get(0);
		}
		return null;
	}

	public int getCountByLikProperties(List properties, List values) {
		int count = userDAO.findCountByLikeProperties(properties, values);
		return count;
	}

	public List searchUserByKeywords(List properties, List values, Page page) {

		// int count = userDAO.findCountByLikeProperties(properties, values);

		List users = userDAO.findByLikeProperties(properties, values,
				page.getPageStartRow(), page.getPageRecorders());

		return users;
	}

	public Organization getOrganization(String orgName) {
		
		List<Organization> orgs = userDAO.findByOrgName(orgName);
		if (orgs != null && orgs.size() > 0) {
			return orgs.get(0);
		}
		return null;
	}

	public Source getSource(long id) {
		Source source = userDAO.findSourceById(id);
		return source;
	}
	
	public List autoComplete(String name) {
		List<String> properties = new ArrayList<String>();
		List values = new ArrayList();
		List users = null;
		String[] names = name.split(" ");
		if (names.length > 0) {
			properties.add("firstName");
			values.add(names[0]);
			if (names.length > 1) {
				properties.add("lastName");
				values.add(names[1]);
			}
			users = userDAO.findByLikeProperties(properties, values, 0, 5);
			
		}
		return users;
	}

}
